using UnityEngine;

public static class Singleton<T> where T : MonoBehaviour
{
    private static T instance;

    public static T Instance
    {
        get
        {
            if (instance) return instance;
            instance = (T) Object.FindObjectOfType(typeof(T));
            if (instance)
            {
                return instance;
            }

            var singleton = new GameObject();
            instance = singleton.AddComponent<T>();
            singleton.name = "(Singleton) " + typeof(T);
            Object.DontDestroyOnLoad(singleton);
            return instance;
        }
    }
}